<?php


namespace addon\hotel\model;

use think\facade\Cache;
use app\model\BaseModel;

/**
 * 房间管理
 * @author Administrator
 *
 */
class Room extends BaseModel
{
    /**
     * 添加房间
     * @param array $data
     */
    public function addRoom($data)
    {
        $ap_id = model('hotel_room')->add($data);
        Cache::tag("hotel_room")->clear();
        return $this->success($ap_id);
    }

    /**
     * 修改房间
     * @param array $data
     */
    public function editRoom($data, $condition)
    {
        $res = model('hotel_room')->update($data, $condition);
        Cache::tag("hotel_room")->clear();
        return $this->success($res);
    }

    /**
     * 删除房间
     * @param array $condition
     */
    public function deleteRoom($condition)
    {
        $res = model('hotel_room')->delete($condition);
        Cache::tag("hotel_room")->clear();
        return $this->success($res);
    }

    /**
     * 获取房间基础信息
     * @param int $ap_id
     * @return multitype:string mixed
     */
    public function getRoomInfo($room_id)
    {
        /*$cache = Cache::get("room_getRoomInfo_" . $room_id);
        if (!empty($cache)) {
            return $this->success($cache);
        }*/
        $res = model('hotel_room')->getInfo([ [ 'room_id', '=', $room_id ] ], '*');
        //Cache::tag("hotel_room")->set("room_getRoomInfo_" . $room_id, $res);
        return $this->success($res);
    }

    public function getNewRoomInfo($condition)
    {

        $res = model('hotel_room')->getInfo($condition, '*');

        return $this->success($res);
    }

    /**
     * 获取房间列表
     * @param array $condition
     * @param string $field
     * @param string $order
     * @param string $limit
     */
    public function getRoomList($condition = [], $field = '*', $order = 'sort desc,room_id desc', $limit = null)
    {
        $data = json_encode([ $condition, $field, $order, $limit ]);
        $cache = Cache::get("room_getRoomList_" . $data);
        if (!empty($cache)) {
            return $this->success($cache);
        }
        $list = model('hotel_room')->getList($condition, $field, $order, '', '', '', $limit);
        Cache::tag("hotel_room")->set("room_getRoomList_" . $data, $list);

        return $this->success($list);
    }

    /**
     * 获取房间分页列表
     * @param array $condition
     * @param number $page
     * @param string $page_size
     * @param string $order
     * @param string $field
     */
    public function getRoomPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'sort desc', $field = '*',$alias='a', $join=[])
    {
        /*$data = json_encode([ $condition, $field, $order, $page, $page_size ]);
        $cache = Cache::get("room_getRoomPageList_" . $data);
        if (!empty($cache)) {
            return $this->success($cache);
        }*/

        $list = model('hotel_room')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
        //Cache::tag("hotel_room")->set("room_getRoomPageList_" . $data, $list);
        return $this->success($list);
    }


    /**
     * 获取房间会员价
     * @param $sku_id
     * @param $member_id
     * @return array
     */
    public function getRoomPrice($room_id, $member_id)
    {
        $res = [
            'member_price' => 0, // 会员价
            'price' => 0 // 最低价格
        ];
        $room_info = model("hotel_room")->getInfo([ [ 'room_id', '=', $room_id ] ], 'price');

        if (empty($room_info)) return $this->success($res);

        $res[ 'price' ] = $room_info[ 'price' ];

        if (!addon_is_exit("memberprice")) return $this->success($res);

        $alias = 'm';
        $join = [
            [ 'member_level ml', 'ml.level_id = m.member_level', 'inner' ],
        ];
        $member_info = model("member")->getInfo([ [ 'member_id', '=', $member_id ] ], 'm.member_card_type,m.member_level,ml.consume_discount,ml.perosn_consume_discount,ml.staff_consume_discount', $alias, $join);
        if (!empty($member_info)) {
                // 默认按会员享受折扣计算
            $discount = 100;
            if ($member_info['member_card_type']==1){
                $discount = $member_info['perosn_consume_discount'];
            } elseif ($member_info['member_card_type']==2){
                $discount = $member_info['consume_discount'];
            } elseif ($member_info['member_card_type']==3){
                $discount = $member_info['staff_consume_discount'];
            }
            $res[ 'member_price' ] = number_format($room_info[ 'price' ] * $discount / 100, 2, '.', '');

            if ($res[ 'member_price' ] < $res[ 'price' ]) {
                $res[ 'price' ] = $res[ 'member_price' ];
            }
        }


        return $this->success($res);
    }
}